/* api */
import useFetch from '@hooks/useFetch'
/* model */
import { SettingTrailApplyModel, SettingTrailSendSmsCodeModel } from '@model/param/in/Setting'
import { settingTrailSendSmsCode, settingTrailApply } from '@src/api/SettingApi'
/* vue */
import { ref, Ref, ComputedRef, computed } from "vue"

const TEL_REG: RegExp = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/

interface WeiXinApplyTrailFeatureDialogFormInterface {
  name: string;
  phone: string;
  code: string;
}

export interface WeiXinApplyTrailFeatureDialogValidateData {
  isError: boolean;
  message: string
}

export class WeiXinApplyTrailFeatureDialogForm implements WeiXinApplyTrailFeatureDialogFormInterface {
  name: string = ''
  phone: string = ''
  code: string = ''
}

export function useWeiXinApplyTrailFeatureForm() {
  // 表单数据
  const form: Ref<WeiXinApplyTrailFeatureDialogForm> = ref(new WeiXinApplyTrailFeatureDialogForm())
  
  function resetForm() {
    form.value = new WeiXinApplyTrailFeatureDialogForm()
  }
  
  function setFormName(name: string) {
    form.value.name = name
  }
  
  function setFormPhone(phone: string) {
    form.value.phone = phone
  }
  
  function setFormCode(code: string) {
    form.value.code = code
  }
  
  
  return {
    form,
    resetForm,
    setFormName,
    setFormPhone,
    setFormCode
  }
}

export function useWeiXinApplyTrailFeatureFormValidate(form: Ref<WeiXinApplyTrailFeatureDialogForm>) {
  
  // 校验数据
  const validateData: Ref<Record<string, WeiXinApplyTrailFeatureDialogValidateData | null>> = ref({})
  
  // 名称是否错误
  const isNameError = computed(() => {
    return validateData.value.name?.isError ?? false
  })
  
  // 手机号码是否错误
  const isPhoneError: ComputedRef<boolean> = computed(() => {
    return validateData.value.phone?.isError ?? false
  })
  
  // 验证码是否错误
  const isCodeError: ComputedRef<boolean> = computed(() => {
    return validateData.value.code?.isError ?? false
  })
  
  function resetValidate() {
    validateData.value = {}
  }
  
  function validateName() {
    
    if (form.value.name) {
      validateData.value.name = null
      return true
    }
    
    validateData.value = {
      ...validateData.value,
      name: {
        isError: true,
        message: '请填写管理员姓名'
      }
    }
  }
  
  function validatePhone() {
    
    if (form.value.phone) {
      
      if (TEL_REG.test(form.value.phone)) {
        validateData.value.phone = null
        return true
      }
      
      validateData.value = {
        ...validateData.value,
        phone: {
          isError: true,
          message: '请填写正确的管理员手机号'
        }
      }
      
      return false
    }
    
    validateData.value = {
      ...validateData.value,
      phone: {
        isError: true,
        message: '请填写管理员手机号'
      }
    }
  }
  
  function validateCode() {
    
    if (form.value.code) {
      validateData.value.code = null
      return true
    }
    
    validateData.value = {
      ...validateData.value,
      code: {
        isError: true,
        message: '请填写验证码'
      }
    }
  }
  
  function validate() {
    const isNameValidated = validateName()
    const isPhoneValidated = validatePhone()
    const isCodeValidated = validateCode()
    
    return isNameValidated && isPhoneValidated && isCodeValidated
  }
  
  
  return {
    validate,
    resetValidate,
    validateName,
    validatePhone,
    validateCode,
    validateData,
    isNameError,
    isPhoneError,
    isCodeError
  }
}

export async function useFetchWeiXinApplyTrailFeatureSendSmsCode(phone: string) {
  
  let params: ComputedRef<SettingTrailSendSmsCodeModel> = useFetchWeiXinApplyTrailFeatureSendSmsCodeParams(phone)
  
  let [data, callback, isLoading, isSuccess, message] = useFetch<SettingTrailSendSmsCodeModel, string>(
    // @ts-ignore
    async () => {
      return settingTrailSendSmsCode(params.value)
    },
    params.value
    )
  
  await callback()
  
  return {
    result: data.value,
    isSuccess: isSuccess.value,
    message
  }
}

export async function useFetchWeiXinApplyTrailFeatureApply(form: WeiXinApplyTrailFeatureDialogForm) {
  
  let params: ComputedRef<SettingTrailApplyModel> = useFetchWeiXinApplyTrailFeatureApplyParams(form)
  
  let [data, callback, isLoading, isSuccess, message] = useFetch<SettingTrailApplyModel, null>(
    // @ts-ignore
    async () => {
      return settingTrailApply(params.value)
    },
    params.value
  )
    
  await callback()
  
  return {
    result: data.value,
    isSuccess: isSuccess.value,
    message
  }
}

export function useFetchWeiXinApplyTrailFeatureSendSmsCodeParams(phone: string): ComputedRef<SettingTrailSendSmsCodeModel> {  
  return computed(() => ({ phone }))
}

export function useFetchWeiXinApplyTrailFeatureApplyParams(form: WeiXinApplyTrailFeatureDialogForm): ComputedRef<SettingTrailApplyModel> {  
  return computed(() => {
    return {
      name: form.name,
      phone: form.phone,
      code: form.code
    }
  })
}